﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIM.model;

namespace LIM.controller
{
    public class DanhMucLienHeController : BaseController
    {
        public DanhMucLienHeController()
        { 
        }
        public bool DeleteLienHeKhachHang(int RowIDLienHe)
        {
            bool Result = true;
            try
            {
                foreach (tbl_CRM_LienHeKhachHang item in LIMS_Entities.tbl_CRM_LienHeKhachHang.Where(p => p.RowIDLienHe == RowIDLienHe).ToList())
                {
                    LIMS_Entities.tbl_CRM_LienHeKhachHang.DeleteObject(item);
                    LIMS_Entities.SaveChanges();
                }
            }
            catch (Exception)
            {
                Result = false;
            }
            return Result;

        }
        public bool SaveLienHeKhachHang(tbl_CRM_LienHeKhachHang lhkh)
        {
            bool Result = true;
            try
            {
                if (lhkh.RowID == 0)
                {
                    LIMS_Entities.tbl_CRM_LienHeKhachHang.AddObject(lhkh);
                }
                LIMS_Entities.SaveChanges();
            }
            catch (Exception)
            {
                Result = false;
            }
            return Result;

        }
        public List<tbl_CRM_KhachHang> LoadKhachHang()
        {
            return LIMS_Entities.tbl_CRM_KhachHang.ToList();
        }
        public List<tbl_DanhMuc_PhongBan> LoadPhongBan()
        {
            return LIMS_Entities.tbl_DanhMuc_PhongBan.ToList();
        }
        public List<tbl_DanhMuc_HeThong> LoadHinhThuc()
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p=>p.RowIDParent == 36).ToList();
        }
        public tbl_CRM_LienHe GetByID(int? RowIDLienHe)
        {
            tbl_CRM_LienHe lienhe = new tbl_CRM_LienHe();
            if (RowIDLienHe != null)
            {
                lienhe = LIMS_Entities.tbl_CRM_LienHe.Where(p => p.RowID == RowIDLienHe).FirstOrDefault();
            }
            return lienhe;

        }
        public object LoadKhachHangLienHe(int RowIDLienHe)
        {
            return LIMS_Entities.tbl_CRM_LienHeKhachHang.Where(p => p.RowIDLienHe == RowIDLienHe).OrderByDescending(p => p.NgayTao).ToList();
        }
        public object LoadHopDongLienHe(int RowIDLienHe)
        {
            return LIMS_Entities.tbl_CRM_HopDong.Where(p => p.RowIDLienLac == RowIDLienHe).OrderByDescending(p => p.NgayTao).ToList().Select(o => new
            {
                KhachHang = GetKHByRowID(o.RowIDKhachHang),
                MaHopDong = o.MaHopDong,
                NgayKy = o.NgayKy,
                GiaTriHopDong = "",
                TinhTrang = GetHinhThucLienLac(o.TinhTrang),
            }).ToList();
        }
        public object LoadNhatKyLienLac(int? RowID)
        {
            List<tbl_CRM_NhatKyLienLac> lst = LIMS_Entities.tbl_CRM_NhatKyLienLac.OrderByDescending(p => p.NgayTao).ToList();
            if (RowID != null && RowID != 0)
            { 
                string username = LIMS_Entities.tbl_DanhMuc_ThongTinNhanVien.Where(p=>p.RowID == RowID).FirstOrDefault().tbl_SYS_Users.UserName;
                lst = lst.Where(p => p.NguoiTao == username).ToList();
            }
          return  lst.Select(o => new
            {
                RowID = o.RowID,
                NhanVien = GetNhanVien(o.NguoiTao),
                HoTenNguoiLienLac = GetByID(o.RowIDLienHe) != null ? GetByID(o.RowIDLienHe).HoTen : "",
                KhachHang = GetKHByRowID(o.RowIDKhachHang),
                HinhThuc = GetHinhThucLienLac(o.MaHinhThuc),
                NoiDung = o.NoiDung,
                KetQua = o.KetQua,
                ThoiGianBatDau = o.ThoiGianBatDau
            }).ToList();
        }
        public object LoadNhatKyLienHe(int RowIDLienHe)
        {
            return LIMS_Entities.tbl_CRM_NhatKyLienLac.Where(p => p.RowIDLienHe == RowIDLienHe).OrderByDescending(p => p.NgayTao).ToList().Select(o => new 
                                                                                                        {
                                                                                                            Ngay = o.NgayTao,
                                                                                                            HinhThucLienLac = GetHinhThucLienLac(o.MaHinhThuc),
                                                                                                            NoiDung = o.NoiDung,
                                                                                                            NhanVien = GetNhanVien(o.NguoiTao),
                                                                                                        }).ToList();
        }
        string GetHinhThucLienLac(string MaDanhmuc)
        {
            string result = "";
            if (MaDanhmuc != null)
            {
                tbl_DanhMuc_HeThong a = LIMS_Entities.tbl_DanhMuc_HeThong.Where(o => o.MaDanhMuc == MaDanhmuc).OrderByDescending(p => p.NgayTao).FirstOrDefault();
                if (a != null)
                {
                    result = a.TenGoi;
                }
            }
            return result;
        }
        string GetNhanVien(string MaNhanVien)
        {
            string result = "";
            if (MaNhanVien != null)
            {
                tbl_SYS_Users a = LIMS_Entities.tbl_SYS_Users.Where(p => p.UserName == MaNhanVien).FirstOrDefault();
                if (a != null)
                {
                    result = a.tbl_DanhMuc_ThongTinNhanVien.HoTen;
                }
            }
            return result;
        }
        public object Load()
        {
            return LIMS_Entities.tbl_CRM_LienHe.ToList().Select(p => new 
                                                            {
                                                                RowID = p.RowID,
                                                                HoTen = p.HoTen,
                                                                XungHo = p.XungHo,
                                                                TenKhachHang = GetKH(p.RowID),
                                                                GhiChu = p.GhiChu,
                                                                SoDiDong = p.SoDiDong,
                                                                Email = p.EmailCaNhan,
                                                                DangConLienLac = p.IsUsed,
                                                            }).ToList();
        }
        string GetKH(int? RowIDLienHe)
        {
            string result = "";
            if (RowIDLienHe != null)
            {
                tbl_CRM_LienHeKhachHang a =LIMS_Entities.tbl_CRM_LienHeKhachHang.Where(o=>o.tbl_CRM_LienHe.RowID == RowIDLienHe).OrderByDescending(p => p.NgayTao).FirstOrDefault();
                if (a != null)
                {
                    result = a.tbl_CRM_KhachHang.TenGoi;
                }
            }
            return result;
        }
        string GetKHByRowID(int? RowID)
        {
            string result = "";
            if (RowID != null)
            {
                tbl_CRM_KhachHang a = LIMS_Entities.tbl_CRM_KhachHang.Where(o => o.RowID == RowID).OrderByDescending(p => p.NgayTao).FirstOrDefault();
                if (a != null)
                {
                    result = a.TenGoi;
                }
            }
            return result;
        }
        public List<tbl_DanhMuc_ThongTinNhanVien> loadNhanVien()
        {
            List<tbl_DanhMuc_ThongTinNhanVien> lst = new List<tbl_DanhMuc_ThongTinNhanVien>();
            lst =  LIMS_Entities.tbl_DanhMuc_ThongTinNhanVien.Where(p=>p.tbl_DanhMuc_PhongBan.MaPhongBan == "KH").ToList();
            tbl_DanhMuc_ThongTinNhanVien nv = new tbl_DanhMuc_ThongTinNhanVien();
            nv.RowID = 0 ;
            nv.HoTen = "--- Tất cả nhân viên EDC-HĐ tạo liên hệ ---";
            lst.Insert(0, nv);
            return lst;
        }
        public List<tbl_CRM_LienHe> LoadLienHe()
        {
            return LIMS_Entities.tbl_CRM_LienHe.OrderByDescending(p=>p.NgayTao).ToList();
        }
        public tbl_CRM_NhatKyLienLac LoadNhatKyLienLacByID(int RowID)
        {
            return LIMS_Entities.tbl_CRM_NhatKyLienLac.Where(p => p.RowID == RowID).FirstOrDefault();
        }
        public tbl_CRM_LienHe LoadLienHeByID(int RowID)
        {
            return LIMS_Entities.tbl_CRM_LienHe.Where(p => p.RowID == RowID).FirstOrDefault();
        }
        public tbl_CRM_KhachHang LoadKhachHangByID(int RowID)
        {
            return LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.RowID == RowID).FirstOrDefault();
        }
    }
}
